<template>
  <!-- 修改性别 -->
  <uni-popup ref="modifySex">
    <uni-popup-dialog
        type="info"
        cancelText="取消"
        confirmText="确认"
        title="修改性别"
        @close="cancel"
        @confirm="submitForm"
        before-close>
      <template v-slot="content">
        <uni-data-checkbox v-model="form.sex" :localdata="dicts.sys_user_sex"></uni-data-checkbox>
      </template>
    </uni-popup-dialog>
  </uni-popup>
</template>

<script>

import {getDictsByNameForUni} from "../../../../utils/dicts";
import {updateSession, updateSex} from "@/api/ec/person/user/userInfo";

export default {
  data() {
    return {
      // 数据字典
      dicts: {
        // 性别
        sys_user_sex: [],
      },
      // 表单
      form: {},
    }
  },
  created() {
    this.getDicts()
  },
  methods: {
    /**加载数据字典*/
    getDicts() {
      // 性别
      this.dicts.sys_user_sex = getDictsByNameForUni("sys_user_sex");
    },
    /** 打开弹出框 */
    toOpenDialog(user) {
      this.form = {sex: user.sex};
      this.$refs.modifySex.open('center')
    },
    /** 取消按钮 */
    cancel() {
      this.$refs.modifySex.close()
    },
    /** 提交按钮 */
    submitForm() {
      let param = {sex: this.form.sex}
      this.$modal.loading();
      updateSex(param).then(response => {
        uni.hideLoading()
        // 刷新登录用户session信息
        updateSession().then((response) => {
          this.$store.dispatch('GetInfo').then((res) => {
            this.$modal.msgSuccess("修改性别成功！");
            setTimeout(() => {
              this.cancel();
              this.$emit('success', "");
            }, 300)
          });
        });
      }).catch(() => {
        uni.hideLoading()
      });
    },
  }
}
</script>

<style lang="scss" scoped>

</style>
